<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/23
 * Time: 12:15
 */

namespace Admin\Controller;


use Think\Controller;

class BatchController extends Controller
{
    public function index()
    {

    }

    //批量操作分配工厂页面
    public function batchBelongGc($ids)
    {
        $id_arr = json_decode($ids, true);
        $omids = implode(",", $id_arr["omids"]);
        $obj = M("user_main");
        $datas = $obj->where("role=4")->select();
        $this->assign("datas", $datas);
        $this->assign("omids", $omids);
        if (isMobile()) {
            $this->display("batchBelongGc_m");
        } else {
            $this->display();
        }
    }

    //批量操作分配工厂表单提交
    public function batchBelongGc2()
    {
        $data = I("post.");
        $obj = M("order_main");
        $obj2 = M("order_profile");
        $omidArr = explode(",", $data["omids"]);

        $gc_updated = time();
        foreach ($omidArr as $key => $value) {
            $obj->belong_gc = $data["gc"];
            $obj->gc_updated = $gc_updated;
            $obj->where("omid=" . $value)->save();
            $obj2->belong_gc = $data["gc"];
            $obj2->gc_updated = $gc_updated;
            $obj2->where("omid=" . $value)->save();
        }
        redirect("../Order/orderList2");
    }

    //批量获取
    public function batchGet()
    {

        $data = I("post.");
        $returnData['status'] = 1;
        $returnData['content'] = json_encode($data);
        $this->ajaxReturn($returnData, 'json');
    }

    //批量打印展示
    public function batchPrint2($id)
    {
        $id_arr = json_decode($id, true);
        $obj = M("order_main");
        $obj2 = M("user_main");
        $print_arrs = array();
        foreach ($id_arr as $key => $value) {
            while (list(, $id) = each($value)) {
                $print_arr = $obj->where("omid=" . $id)->find();
                $uid = $print_arr["uid"];
                $print_arr2 = $obj2->where("uid=" . $uid)->find();
                $print_arr3 = array_merge($print_arr, $print_arr2);
                $print_arrs[] = $print_arr3;
            }
        }
        $this->assign("print_arrs", $print_arrs);
        $this->display("Print/printOrderBatch");
    }

    //批量完成配送
    public function batchPsEnd($id)
    {
        $id_arr = json_decode($id, true);
        $updated = time();
        $obj = M("order_main");
        $obj2 = M("order_profile");
        foreach ($id_arr as $da) {
            foreach ($da as $ids) {
                $obj->status = 4;
                $obj->updated = $updated;
                $rs = $obj->where("omid=" . $ids)->save();
                if ($rs == false) {
                    $this->error("失败", "../../psList");
                } else {
                    $obj2->status = 4;
                    $obj2->updated = $updated;
                    $rs2 = $obj2->where("omid=" . $ids)->save();
                    if ($rs2 == false) {
                        $this->error("失败", "../../psList");
                    }
                }
            }
        }
        redirect("../../../Order/psList");
    }

    //工厂批量制作完成
    public function batchEndGc($ids)
    {
        $oidArr = json_decode($ids, true);
        $obj = M("order_main");
        $obj2 = M("order_profile");
        $updated = time();
        foreach ($oidArr as $oids) {
            foreach ($oids as $oid) {
                //修改子订单
                $data['status'] = 2;
                $data['updated'] = $updated;
                $obj2->where("oid=" . $oid)->save($data);
                $arr2 = $obj2->where("oid=" . $oid)->find();
                $omid = $arr2["omid"];
                //修改主订单
                $data2['status'] = 2;
                $data2['updated'] = $updated;
                $obj->where("omid=" . $omid)->save($data2);
            }
        }
        redirect("../../../Order/orderList4");
    }
    //工厂批量返工完成
    public function batchReturnEndGc($ids)
    {
        $oidArr = json_decode($ids, true);
        $obj = M("order_main");
        $obj2 = M("order_profile");
        $updated = time();
        foreach ($oidArr as $oids) {
            foreach ($oids as $oid) {
                //修改子订单
                $data['status'] = 6;
                $data['updated'] = $updated;
                $rs2 = $obj2->where("oid=" . $oid)->save($data);
                if ($rs2 == false) {
                    $this->error("提交失败", "../../returnList_gc");
                } else {
                    $arr2 = $obj2->where("oid=" . $oid)->find();
                    $omid = $arr2["omid"];

                    //修改主订单
                    $data2['status'] = 6;
                    $data2['updated'] = $updated;
                    $obj->where("omid=" . $omid)->save($data2);
                }
            }
        }
        $this->success("提交成功", "../../../Order/returnList_gc");
    }

    //批量配送
    public function batchPs($ids){
        $omidArr = json_decode($ids,true);
        $omids = implode(",",$omidArr["omids"]);
        $obj = M("user_main");
        $ps_datas = $obj->where("role=5")->select();
        $this->assign("ps_datas",$ps_datas);
        $this->assign("omids", $omids);
        if (isMobile()) {
            $this->display("batchPs_m");
        } else {
            $this->display();
        }
    }
    //批量配送时间提交
    public function batchPs2(){
        $data = I("post.");
        $omidArr = explode(",", $data["omids"]);
        $obj = M("order_main");
        $obj2 = M("order_profile");
       foreach($omidArr as $key=>$value){
           $mArr["status"] = 3;
           $mArr["ps_date"] = $data["ps_date"];
           $mArr["ps"] = $data["ps"];
           $mArr["ps_yuan"]=$data["ps_yuan"];
           $rs = $obj->where("omid=" . $value)->save($mArr);
           if ($rs == false) {
               $this->error("配送失败", "../../endList");
           } else {
               $pArr["status"] = 3;
               $obj2->where("omid=" . $value)->save($pArr);
           }
       }
        redirect("../Order/endList");
    }
    //批量分配配送员页面
    public function batchPsBelong($ids){
        $omidArr = json_decode($ids,true);
        $omids = implode(",",$omidArr["omids"]);
        $obj = M("user_main");
        $datas = $obj->where("role=5")->select();
        $this->assign("datas",$datas);
        $this->assign("omids", $omids);
        $this->display();
    }
    //批量分配配送员
    public function batchPsBelong2(){
        $data = I("post.");
        $omidArr = explode(",", $data["omids"]);
        $obj = M("order_main");
        foreach($omidArr as $omid){
            $arr["ps_yuan"] = $data["ps_yuan"];
            $obj->where("omid=".$omid)->save($arr);
        }
        redirect("../Order/psList");
    }
} 